<?php
//报价单
class QuotesController extends Controller
{

    public function actionIndex()
    {
        $this->render('index');
    }

    public function actionGetdata(){

        $page = $_REQUEST['page'];
        $rows = $_REQUEST['rows'];
        $start = ($page-1)*$rows;
        $total = $this->connection->createCommand("select count(1) from crm_quotes where deleted = 0 ")->queryScalar();
        $result = $this->connection->createCommand("select c.*,u.realname from crm_quotes c left join crm_potentials
 p on c.potentials_id = p.potentials_id where c.deleted = 0  limit {$rows} offset {$start}")->queryAll();

        $data = Array();
        $data['total'] = $total;
        $data['rows'] = $result;

        echo json_encode($data);
    }

    public function actionShowedit(){

        $quotes_id = $_REQUEST['quotes_id'];

        $quotes = null;
        if($quotes_id==0){
            //新增
        }else{
            //编辑
            $quotes = $this->connection->createCommand("select c.*,u.realname from crm_quotes c left join 
 crm_potentials p on c.potentials_id = p.potentials_id  where c.quotes_id = :quotes_id")->bindParam(':quotes_id',$quotes_id,PDO::PARAM_INT)->queryRow();
        }

        $this->render('showedit',['quotes'=>$quotes]);
    }

    public function actionSavedata(){

        $quotes_id = $_REQUEST['quotes_id'];
        $quotes_number = $_REQUEST['quotes_number'];
        $stage = $_REQUEST['stage'];
        $potentials_id = $_REQUEST['potentials_id'];
        $total = $_REQUEST['total'];
        $quotetime = $_REQUEST['quotetime'];

        if($quotes_id!=null){
            //编辑
            $this->connection->createCommand("update crm_quotes set quotes_number = :quotes_number,potentials_id = :potentials_id
,stage = :stage,total = :total,quotetime = :quotetime
where quotes_id = :quotes_id")
                ->bindParam(':quotes_id',$quotes_id,PDO::PARAM_INT)
                ->bindParam(':quotes_number',$quotes_number,PDO::PARAM_STR)
                ->bindParam(':stage',$stage,PDO::PARAM_STR)
                ->bindParam(':potentials_id',$potentials_id,PDO::PARAM_STR)
                ->bindParam(':total',$total,PDO::PARAM_STR)
                ->bindParam(':quotetime',$quotetime,PDO::PARAM_STR)
                ->execute();
        }else{
            //添加
            $this->connection->createCommand("insert into crm_quotes (quotes_number,potentials_id,stage,total,quotetime,nextstep) values 
(:quotes_number,:potentials_id,:stage,:total,:quotetime) ")
                ->bindParam(':quotes_number',$quotes_number,PDO::PARAM_STR)
                ->bindParam(':stage',$stage,PDO::PARAM_STR)
                ->bindParam(':potentials_id',$potentials_id,PDO::PARAM_STR)
                ->bindParam(':total',$total,PDO::PARAM_STR)
                ->bindParam(':quotetime',$quotetime,PDO::PARAM_STR)
                ->execute();
        }
    }


    public function actionRemove(){
        $quotes_id = $_REQUEST['quotes_id'];
        $this->connection->createCommand("update crm_quotes set deleted = 1 
where quotes_id = :quotes_id")
            ->bindParam(':quotes_id',$quotes_id,PDO::PARAM_INT)
            ->execute();
    }

}